<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">
</head>

<body>

<div class="nav">
    五⼦棋对战
</div>
<div class="login-container">
    <div class="login-dialog">

        <h3>登录</h3>
        <!-- 输⼊⽤⼾名 -->
        <div class="row">
            <span>⽤⼾名</span>
            <input type="text" id="username" name="username">
        </div>

        <!-- 输⼊密码 -->
        <div class="row">
            <span>密码</span>
            <input type="password" id="password" name="password">
        </div>

        <!-- 提交按钮 -->
        <div class="row submit-row">
            <button id="submit">提交</button>
        </div>

    </div>
</div>

<script src="./js/jquery.min.js"></script>
<script>
    // 通过 ajax 的⽅式实现登录过程
    let submitButton = document.querySelector('#submit');
    submitButton.onclick = function() {
        // 1. 先获取到⽤⼾名和密码
        let username = document.querySelector('#username').value;
        let password = document.querySelector('#password').value;
        $.ajax({
            method: 'post',
            url: '/login',
            data: {
                username: username,
                password: password
            },
            success: function(data) {
                console.log(JSON.stringify(data));
                if (data && data.userId > 0) {
                    // 登录成功, 跳转到游戏⼤厅
                    alert("登录成功!")
                    location.replace('/game_hall.html');
                } else {
                    alert("登录失败! ⽤⼾名密码错误! 或者该账号正在游戏中!");
                }
            }
        });
    }
</script>

</body>
</html>